package jrain.fw.dao.common.parser.bean;

import java.util.Map;

import jrain.utils.exception.ServiceException;
import jrain.utils.lang.StringUtils;

public class ConditionBean {

	private Object value1;
	private Object value2;
	private String op;

	public Object getValue1() {
		return value1;
	}

	public void setValue1(Object value1) {
		this.value1 = value1;
	}

	public Object getValue2() {
		return value2;
	}

	public void setValue2(Object value2) {
		this.value2 = value2;
	}

	public String getOp() {
		return op;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public boolean calc(Map<String, Object> paramData) {
		if (value1 != null && value1 instanceof String) {
			String str1 = (String) value1;
			boolean bvar = !(str1.startsWith("'") && str1.endsWith("'"));
			// 是否整数判定
			bvar = bvar && !str1.matches("\\d+");
			if (bvar) {
				value1 = paramData.get(str1);
			}
		}
		if (!("is".equals(op) || "isnot".equals(op))) {
			if (value2 != null && value2 instanceof String) {
				String str2 = (String) value2;
				boolean bvar = !(str2.startsWith("'") && str2.endsWith("'"));
				bvar = bvar && !str2.matches("\\d+");
				if (!(str2.startsWith("'") && str2.endsWith("'"))) {
					value2 = paramData.get(str2);
				}
			}
		}

		if (op.equals("==")) {
			return equals();
		} else if (op.equals("!=")) {
			return notEquals();
		} else if (op.equals("is")) {
			return isCalc();
		} else if (op.equals("isnot")) {
			return !isCalc();
		} else {
			throw new ServiceException("invalid operator!,op=" + op);
		}
	}

	public boolean equals() {
		if (value1 == null && value2 == null) {
			return true;
		} else if (value1 == null && value2 != null) {
			return false;
		} else if (value1 != null && value2 == null) {
			return false;
		} else {
			if (value1 instanceof Integer) {
				if (value2 instanceof Integer) {
					return value1 == value2;
				} else if (value2 instanceof Long) {
					return value1 == value2;
				} else if (value2 instanceof String) {
					return value2.equals(String.valueOf(value1));
				} else {
					return false;
				}
			} else if (value1 instanceof Long) {
				if (value2 instanceof Integer) {
					return value1 == value2;
				} else if (value2 instanceof Long) {
					return value1 == value2;
				} else if (value2 instanceof String) {
					return value2.equals(String.valueOf(value1));
				} else {
					return false;
				}
			} else if (value1 instanceof String) {
				if (value2 instanceof Integer) {
					return value1.equals(String.valueOf(value2));
				} else if (value2 instanceof Long) {
					return value1.equals(String.valueOf(value2));
				} else if (value2 instanceof String) {
					return value1.equals(value2);
				} else {
					return false;
				}
			} else {
				return false;
			}
		}
	}

	public boolean notEquals() {
		return !equals();
	}

	public boolean isCalc() {
		if ("empty".equals(value2)) {
			return StringUtils.isEmpty(value1);
		} else if ("zero".equals(value2)) {
			return StringUtils.isEmptyZero(value1);
		} else {
			throw new ServiceException("invalid operator!,op=" + value2);
		}
	}

}
